const puppeteer = require("puppeteer");


const  requestHandler = async (request) => {
    // console.log(request.url());
    request.continue();
    // if (request.resourceType() === "font") {
    //     request.abort();
    // } else {
    //     request.continue();
    // }
};
async function main() {
    // 
    const browser = await puppeteer.launch({headless: false});
    const page = await browser.newPage();
    const userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36';
    await page.setUserAgent(userAgent);
    // await page.setRequestInterception(true);

    // page.on("request", requestHandler);

    // await page.setCookie({
    //     name: 'userId',
    //     value: "2813741782",
    //     domain: 'id.kuaishou.com',
    //     path: '/',
    //     expires: Date.now() + 86400000, 
    //     secure: true, // 仅在 HTTPS 连接上发送 cookie
    //     httpOnly: true 
    // });

    // await page.setCookie({
    //     name: 'userId',
    //     value: "2813741782",
    //     domain: '.kuaishou.com',
    //     path: '/',
    //     expires: Date.now() + 86400000, 
    //     secure: true, // 仅在 HTTPS 连接上发送 cookie
    //     httpOnly: true 
    // });

    // // 设置cookie
    // const cookie = "ChNwYXNzcG9ydC5wYXNzLXRva2VuEpABKvL2dMdyikNFrpjG_g0WZjwFwZlmrPpteWR6ygUJMVtxWOdz2hor-Wn-RTmLrYirHUD6blBxiCBAtUpC10HSIg2WrfZwXeEbEl2zSolsbQpSGtmSEbyiWhlliYTPyKZs_YLmjfPfOmnTaqGDF8vBjBxQbk0Navyt3FA-dqNrT3JpxdfrtuzImx9We57xl5JZGhLmcxJU_iJJ_bzbaUG-W3aMOLciIJZW9mijih-HXUfGnoBNVQvjyE5B85KtfkYNjdKCtMDPKAUwAQ";
    // await page.setCookie({
    //     name: 'passToken',
    //     value: cookie,
    //     domain: 'id.kuaishou.com',
    //     path: '/',
    //     expires: Date.now() + 86400000, 
    //     secure: true, // 仅在 HTTPS 连接上发送 cookie
    //     httpOnly: true 
    // });

    await page.goto('https://www.kuaishou.com/short-video/3xsgn4pjbpgk8yu?authorId=3xnbmb2reg8nrtm&streamSource=profile&area=profilexxnull');
    // / Set screen size
    await page.setViewport({width: 1920, height: 1024});

    // 查找评论按钮 点击
    // await page.waitForSelector('.lottie-item');
    // const elements = await page.$$('.lottie-item');
    // await elements[1].click();
    // await page.waitForFrame

    // const frameHandle = await page.$('iframe'); // 获取iframe元素的句柄
    // if (frameHandle) {
    //     const frame = await frameHandle.contentFrame();
    //     // 如果有安全验证  进行关闭
    //     if (frame) {
    //         await frame.waitForSelector('.icon-close');
    //         await frame.click('.icon-close');
    //         await page.waitForFunction(() => !document.querySelector('iframe'));
    //     }
    // }

    // await page.waitForSelector('.short-video-info', { visible: true });
    // await page.waitForTimeout(5 * 1000);
    // // // 输入评论
    // await page.waitForSelector('.comment-container>.comment-input>.input-container>.pl-textarea');
    // await page.type('.comment-container>.comment-input>.input-container>.pl-textarea', "厉害呀");

    // // const content = await page.content();
    // // console.log(content);
    // // 点击发送
    // await page.waitForSelector(".comment-container>.comment-input>.input-container>.submit-button");
    // // const content = await page.content();
    // // console.log(content);
    // await page.click(".comment-container>.comment-input>.input-container>.submit-button");
    // console.log("send");
    // // 弹窗
    // await page.waitForSelector('.modal-dialog');
    // console.log("login");
    // browser.close();
    // await page.type('.pl-textarea', "test");
    // await page.waitForSelector('.pl-textarea').click();
    // await page.waitForSelector('.login-btn a');
    // await page.click('.login-btn a');

    // console.log("popu login");

    // const frameHandle = await page.$('iframe'); // 获取iframe元素的句柄
    // const frame = await frameHandle.contentFrame(); // 获取iframe的

    // await frame.waitForSelector('.third-login a');
    // await frame.click('.login-btn a');
    // await frame.type(, "")
//   await page.click('input[type=submit]');

//   // Locate the full title with a unique string
//   const textSelector = await page.waitForSelector(
//     'text/Customize and automate'
//   );
//   const fullTitle = await textSelector?.evaluate(el => el.textContent);

//   // Print the full title
//   console.log('The title of this blog post is "%s".', fullTitle);
}

main();